package com.example.android_learn;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleRegistry;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    MainActivityViewModel mainActivityViewModel;

    Button button;
    TextView textView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        doObserver();
    }

    private void init() {
        // 设立view model
        mainActivityViewModel = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(MainActivityViewModel.class);
        initView();
        initOperations();
    }

    private void initView() {
        button = findViewById(R.id.button);
        textView = findViewById(R.id.textView);
    }

    private void initOperations() {
        initData();
    }

    private void initData() {

        // 参数的this来源于被包裹的类
        mainActivityViewModel.liveData.observe(this, new Observer<String>() {

            @Override
            public void onChanged(String s) {
                textView.setText(s);
            }
        });

//        LiveDataUtils.setValue(mainActivityViewModel.liveData, "fuckong kkkkk");

        mainActivityViewModel.setLiveData();
    }


    private void doObserver() {
        getLifecycle().addObserver(new MyObserver(this));
    }

    int x = 123;

}
